#!/bin/sh

# This script is part of Bakefile (http://www.bakefile.org) autoconf
# script. It is used to track C/C++ files dependencies in portable way.
#
# Permission is given to use this file in any way.

DEPSMODE=@DEPSMODE@
DEPSFLAG="@DEPSFLAG@"
DEPSDIRBASE=.deps

if test $DEPSMODE = gcc ; then
    $* ${DEPSFLAG}
    status=$?

    # determine location of created files:
    while test $# -gt 0; do
        case "$1" in
            -o )
                shift
                objfile=$1
            ;;
            -* )
            ;;
            * )
                srcfile=$1
            ;;
        esac
        shift
    done
    objfilebase=`basename $objfile`
    builddir=`dirname $objfile`
    depfile=`basename $srcfile | sed -e 's/\..*$/.d/g'`
    depobjname=`echo $depfile |sed -e 's/\.d/.o/g'`
    depsdir=$builddir/$DEPSDIRBASE
    mkdir -p $depsdir

    # if the compiler failed, we're done:
    if test ${status} != 0 ; then
        rm -f $depfile
        exit ${status}
    fi

    # move created file to the location we want it in:
    if test -f $depfile ; then
        sed -e "s,$depobjname:,$objfile:,g" $depfile >${depsdir}/${objfilebase}.d
        rm -f $depfile
    else
        # "g++ -MMD -o fooobj.o foosrc.cpp" produces fooobj.d
        depfile=`echo "$objfile" | sed -e 's/\..*$/.d/g'`
        if test ! -f $depfile ; then
            # "cxx -MD -o fooobj.o foosrc.cpp" creates fooobj.o.d (Compaq C++)
            depfile="$objfile.d"
        fi
        if test -f $depfile ; then
            sed -e "\,^$objfile,!s,$depobjname:,$objfile:,g" $depfile >${depsdir}/${objfilebase}.d
            rm -f $depfile
        fi
    fi
    exit 0

elif test $DEPSMODE = mwcc ; then
    $* || exit $?
    # Run mwcc again with -MM and redirect into the dep file we want
    # NOTE: We can't use shift here because we need $* to be valid
    prevarg=
    for arg in $* ; do
        if test "$prevarg" = "-o"; then
            objfile=$arg
        else
            case "$arg" in
                -* )
                ;;
                * )
                    srcfile=$arg
                ;;
            esac
        fi
        prevarg="$arg"
    done

    objfilebase=`basename $objfile`
    builddir=`dirname $objfile`
    depsdir=$builddir/$DEPSDIRBASE
    mkdir -p $depsdir

    $* $DEPSFLAG >${depsdir}/${objfilebase}.d
    exit 0

elif test $DEPSMODE = unixcc; then
    $* || exit $?
    # Run compiler again with deps flag and redirect into the dep file.
    # It doesn't work if the '-o FILE' option is used, but without it the
    # dependency file will contain the wrong name for the object. So it is
    # removed from the command line, and the dep file is fixed with sed.
    cmd=""
    while test $# -gt 0; do
        case "$1" in
            -o )
                shift
                objfile=$1
            ;;
            * )
                eval arg$#=\$1
                cmd="$cmd \$arg$#"
            ;;
        esac
        shift
    done

    objfilebase=`basename $objfile`
    builddir=`dirname $objfile`
    depsdir=$builddir/$DEPSDIRBASE
    mkdir -p $depsdir

    eval "$cmd $DEPSFLAG" | sed "s|.*:|$objfile:|" >${depsdir}/${objfilebase}.d
    exit 0

else
    $*
    exit $?
fi
